package com.cloud.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.feignclient.GoodsFeignClient;
import com.cloud.mall.goods.TbItem;
import com.cloud.mall.goods.dto.ItemStockDto;
import com.cloud.mall.order.TbOrder;
import com.cloud.mall.order.TbOrderItem;
import com.cloud.mall.order.dto.OrderDto;
import com.cloud.mall.order.dto.OrderItemDto;
import com.cloud.mapper.TbOrderMapper;
import com.cloud.service.ITbOrderItemService;
import com.cloud.service.ITbOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author jobob
 * @since 2021-07-05
 */

@Service
public class TbOrderServiceImpl extends ServiceImpl<TbOrderMapper, TbOrder> implements ITbOrderService {
    @Autowired
    GoodsFeignClient goodsFeignClient;
    @Autowired
    ITbOrderItemService iTbOrderItemService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public String order(OrderDto orderDto) {
        List<OrderItemDto> items = orderDto.getItems();
        List<ItemStockDto> requestList = items.stream().map(item -> {
            ItemStockDto itemStockDto = new ItemStockDto();
            itemStockDto.setItemId(item.getItemId());
            itemStockDto.setNum(item.getNum());
            return itemStockDto;
        }).collect(Collectors.toList());
        String orderId = UUID.randomUUID().toString();
        if (goodsFeignClient.decreaseGoodsAmount(requestList)) {
            List<Long> ids = items.stream().map(item -> {
                return item.getItemId();
            }).collect(Collectors.toList());
            List<TbItem> tbItems = goodsFeignClient.listByIds(ids);

            List<TbOrderItem> tbOrderItems =
                    tbItems.stream().map(tbItem -> {
                        TbOrderItem tbOrderItem = new TbOrderItem();
                        tbOrderItem.setCreateTime(LocalDateTime.now());
                        tbOrderItem.setId(UUID.randomUUID().toString());
                        tbOrderItem.setCreateTime(LocalDateTime.now());
                        tbOrderItem.setItemId(tbItem.getId());
                        tbOrderItem.setOrderId(orderId);
                        tbOrderItem.setTitle(tbItem.getTitle());
                        tbOrderItem.setUpdateTime(LocalDateTime.now());
                        tbOrderItem.setTotalFee(new BigDecimal(0));

                        items.forEach(front -> {
                            if (front.getItemId().equals(tbItem.getId())) {
                                int num = front.getNum();
                                tbOrderItem.setNum(num);
                                BigDecimal bigDecimal = tbItem.getPrice().multiply(new BigDecimal(num)).setScale(4, BigDecimal.ROUND_HALF_UP);
                                tbOrderItem.setPrice(bigDecimal);
                            }
                        });
                        return tbOrderItem;
                    }).collect(Collectors.toList());

            iTbOrderItemService.saveBatch(tbOrderItems);

            TbOrder tbOrder = new TbOrder();
            tbOrder.setUserId(orderDto.getUserId());
            tbOrder.setCloseTime(LocalDateTime.now());
            tbOrder.setConsignTime(LocalDateTime.now());
            tbOrder.setCreateTime(LocalDateTime.now());
            tbOrder.setStatus(0);
            tbOrder.setOrderId(orderId);

            BigDecimal total = new BigDecimal(0);
            for (TbOrderItem tbOrderItem : tbOrderItems) {
                total = total.add(tbOrderItem.getPrice());
            }
            tbOrder.setPayment(total);
            tbOrder.setPaymentType(0);
            this.save(tbOrder);
        }
        return orderId;
    }
}
